package blobDetector;

import hdgeom.primitives.AbstractPolygon;

import java.util.Vector;

import processing.core.PVector;

public class Blob extends AbstractPolygon {
	int minX;
	int minY;
	int maxX;
	int maxY;
	Vector<BlobCell> borders;

	public Blob() {
		borders = new Vector<BlobCell>();
		minX = Integer.MAX_VALUE;
		maxX = -10000000;
		minY = Integer.MAX_VALUE;
		maxY = -10000000;
	}

	public boolean contains(int x, int y) {
		int n = 0;
		for (int i = 0; i < borders.size(); i++) {
			BlobCell bCell = borders.get(i);
			if (bCell.x == x && bCell.y > y) {
				n++;
			}
		}
		if (n % 2 == 1)
			return true;
		else
			return false;
	}

	public boolean[][] containsMatrix() {
		boolean[][] inside = new boolean[maxX - minX + 1][maxY - minY + 1];
		boolean[][] border = new boolean[maxX - minX + 1][maxY - minY + 1];
		for (int i = 0; i < borders.size(); i++) {
			BlobCell bCell = borders.get(i);
			border[bCell.x - minX][bCell.y - minY] = true;
			inside[bCell.x - minX][bCell.y - minY] = true;
		}
		for (int x = 0; x < inside.length; x++) {
			boolean flag = false;
			for (int y = 0; y < inside[x].length - 1; y++) {
				if (y == 0 && border[x][y]) {
					flag = true;
				}
				if (flag || border[x][y]) {
					inside[x][y] = true;
				}
				if (border[x][y] == false && border[x][y + 1] == true) {
					flag = !flag;
				}
			}

		}
		return inside;
	}

	public int getMinX() {
		return minX;
	}

	public void setMinX(int minX) {
		this.minX = minX;
	}

	public int getMinY() {
		return minY;
	}

	public void setMinY(int minY) {
		this.minY = minY;
	}

	public int getMaxX() {
		return maxX;
	}

	public void setMaxX(int maxX) {
		this.maxX = maxX;
	}

	public int getMaxY() {
		return maxY;
	}

	public void setMaxY(int maxY) {
		this.maxY = maxY;
	}

	public Vector<BlobCell> getBorders() {
		return borders;
	}

	public int getWidth() {
		return maxX - minX;
	}

	public int getHeight() {
		return maxY - minY;
	}

	public void setBorders(Vector<BlobCell> borders) {
		this.borders = borders;
	}

	public void add(int x, int y) {
		borders.add(new BlobCell(x, y));
		minX = Math.min(minX, x);
		minY = Math.min(minY, y);
		maxX = Math.max(maxX, x);
		maxY = Math.max(maxY, y);
	}

	@Override
	public float getX(int i) {
		// TODO Auto-generated method stub
		return borders.get(i).x;
	}

	@Override
	public float getY(int i) {
		// TODO Auto-generated method stub
		return borders.get(i).y;
	}

	@Override
	public int getNumVertices() {
		// TODO Auto-generated method stub
		return borders.size();
	}

	@Override
	public void getPosition(int i, float[] coords) {
		// TODO Auto-generated method stub
		BlobCell blob = borders.get(i);
		coords[0] = blob.x;
		coords[1] = blob.y;
		coords[2] = 0;
	}

	@Override
	public PVector getVertex(int i) {
		// TODO Auto-generated method stub
		BlobCell blob = borders.get(i);
		return new PVector(blob.x, blob.y);
	}

	@Override
	public void setPosition(int i, float x, float y, float z) {
		// TODO Auto-generated method stub
		BlobCell blob = borders.get(i);
		blob.x = (int) x;
		blob.y = (int) y;
	}

	@Override
	public void setPosition(int i, float x, float y) {
		// TODO Auto-generated method stub
		BlobCell blob = borders.get(i);
		blob.x = (int) x;
		blob.y = (int) y;
	}
}
